<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>食堂数据展示</title>
    <link rel="stylesheet" href="plugins/layui/css/layui.css" media="all" />
    <link rel="stylesheet" href="css/comments.css" media="all">
    <style>
        .layui-form-label{
            width: 120px;
        }
        @media screen and (max-width: 450px){
            .layui-form-item .layui-input-inline {
                margin-left: 150px;
            }
        }
        
    </style>
</head>
<body>
    <div class="maincontainer">
        <div class="layui-card">
            <div class="layui-card-header layuiadmin-card-header-auto">
                <form id="queryForm" class="layui-form" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">统计时间</label>
                            <div class="layui-input-inline">
                                <input type="text" class="layui-input" id="selectTime" placeholder="">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">食堂名称</label>
                            <div class="layui-input-inline">
                                <select name="canteenId" lay-filter="canteenId">
                                    <option value=""></option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="button" class="layui-btn"  lay-submit lay-filter="searchBth">查询</button>
                            <button type="reset" class="layui-btn layui-btn-primary" >重置</button>
                        </div>
                    </div>
                </form>
            </div>
            <div class="layui-card-body">
                <div class="layui-row">
                    <div class="layui-col-xs12 layui-col-sm6 layui-col-md6 layui-col-lg6">
                        <div id="main1" style="width: 600px;height:400px;"></div>
                    </div>
                    <div class="layui-col-xs12 layui-col-sm6 layui-col-md6 layui-col-lg6">
                        <div id="main2" style="width: 600px;height:400px;"></div>
                    </div>
                    <div class="layui-col-xs12 layui-col-sm6 layui-col-md6 layui-col-lg6">
                        <div id="main3" style="width: 600px;height:400px;"></div>
                    </div>
                  </div>
            </div>
        </div>
    </div>
</body>

<script type="text/javascript" src="plugins/layui/layui.js"></script>
<script type="text/javascript" src="js/jquery.min.js"></script>
<script src="js/echarts.common.min.js"></script>
<script>
    layui.config({
        base: 'plugins/layui/modules/',
    }).use(['http', 'laydate', 'form'], function() {
        var http = layui.http,
            laydate = layui.laydate,
            form = layui.form;

        //时间
        laydate.render({
            elem: '#selectTime'
            ,theme: '#fbaf7e'
            ,type: 'month'
        });
        
        // 食堂名称
        http.get('admin/canteen/listSimplePage/observer',{},function(res){
            if(res.code == 200){
                var html = '<option value=""></option>';
				var data = res.data;
				for (var i = 0; i < data.length; i++) {
					html += '<option value="' + data[i].id + '">' + data[i].canteenName + '</option>'
				}
				$('select[name=canteenId]').html(html);
				form.render('select');
            }else{
                layer.msg(res.msg)
            }
        })

        // 统计
        var myChart1 = echarts.init(document.getElementById('main1'));
        var myChart2 = echarts.init(document.getElementById('main2'));
        var myChart3 = echarts.init(document.getElementById('main3'));
        chartFun()
        function chartFun(startTime,endTime,canteenId){
            http.get('admin/count/countOrder/canteen',{
                startTime: startTime,
                endTime: endTime,
                canteenId: canteenId
            },function(res){
                if(res.code == 200){
                    var xAxisData = []
                    var seriesData1 = [],seriesData2 = [],seriesData3 = []
                    for(var i=0;i<res.data.length;i++){
                        xAxisData.push(res.data[i].time)
                        seriesData1.push(res.data[i].orderCount)
                        seriesData2.push(res.data[i].orderTotalMoney)
                        seriesData3.push(res.data[i].cancelOrderMoney)
                    }
                    chart1Fun(xAxisData,seriesData1)
                    chart2Fun(xAxisData,seriesData2)
                    chart3Fun(xAxisData,seriesData3)
                    
                }else{
                    layer.msg(res.msg)
                }
            })

        }
        // 食堂下单数量
        function chart1Fun(xAxisData,seriesData){
            var option = {
                title: {
                    text: '食堂下单数量'
                },
                tooltip: {
                    trigger: 'item',
                    formatter: '{b}: {c} '
                },
                color: ['#f56643'],
                dataZoom: [
                    {
                        type: 'slider',
                        show: true,
                    },
                ],
                xAxis: {
                    type: 'category',
                    data: xAxisData
                },
                yAxis: {
                    type: 'value'
                },
                series: [{
                    data: seriesData,
                    type: 'bar'
                }]
            };
            
            // console.log(option)
            myChart1.setOption(option);
        }
        // 食堂下单金额
        function chart2Fun(xAxisData,seriesData){
            var option = {
                title: {
                    text: '食堂下单金额'
                },
                tooltip: {
                    trigger: 'item',
                    formatter: '{b}: ￥{c} '
                },
                color: ['#f56643'],
                dataZoom: [
                    {
                        type: 'slider',
                        show: true,
                    },
                ],
                xAxis: {
                    type: 'category',
                    data: xAxisData
                },
                yAxis: {
                    type: 'value'
                },
                series: [{
                    data: seriesData,
                    type: 'bar'
                }]
            };
            myChart2.setOption(option);
        }
        // 已取消订单金额
        function chart3Fun(xAxisData,seriesData){
            var option = {
                title: {
                    text: '已取消订单金额'
                },
                tooltip: {
                    trigger: 'item',
                    formatter: '{b}: ￥{c} '
                },
                color: ['#f56643'],
                dataZoom: [
                    {
                        type: 'slider',
                        show: true,
                    },
                ],
                xAxis: {
                    type: 'category',
                    data: xAxisData
                },
                yAxis: {
                    type: 'value'
                },
                series: [{
                    data: seriesData,
                    type: 'bar'
                }]
            };
            myChart3.setOption(option);
        }

        //查询
        form.on('submit(searchBth)', function (data) {
            var time = $('#selectTime').val()
            var canteenId = data.field.canteenId
            if(time){
                var day = new Date(time.split('-')[0],time.split('-')[1],0);   //最后一个参数为0,意为获取2018年10月一共多少天
                var date = day.getDate()
                chartFun(time+'-1',time+'-'+date,canteenId)
            }else{
                chartFun('','',canteenId)
            }
        })


    })

</script>
</html>